#include "standby.h"
uint32_t rtcWakeUpCnt =0;
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
  rtcWakeUpCnt++;
}

void EnterSTANDBYMode(int8_t WakeUpPin)
{
  __HAL_RCC_APB2_FORCE_RESET();
  __HAL_RCC_PWR_CLK_ENABLE();
  /* Clear all related wakeup flags */
  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  if(WakeUpPin & 0x01)
  {
    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
  }
  else
  {
    HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
  }
  if(WakeUpPin & 0x02)
  {
    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2);
  }
  else
  {
    HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN2);
  }
  /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
  HAL_PWR_EnterSTANDBYMode();
}

int BringWakeUp()
{
  int rt = PowerOn;
  if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
    {
      /* Clear Standby flag */
      __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
      
      /* Clear all related wakeup flags */
      __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
      
      rt = WakeUp;
    }

    return rt;
}


void SystemPower_Config(void)
{
  /* Enable Ultra low power mode */
  HAL_PWREx_EnableUltraLowPower();
  
  /* Enable the fast wake up from Ultra low power mode */
  HAL_PWREx_EnableFastWakeUp();
  #ifndef __RELEASE__
  HAL_DBGMCU_EnableDBGStandbyMode();
  #else
  HAL_DBGMCU_DisableDBGStandbyMode();
  #endif
}


